package com.hrbxlh.websocket;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.hrbxlh.dao.VirtualMachineManageDao;
import com.hrbxlh.exception.OpenstackException;
import com.hrbxlh.util.OpenstackUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;


/**
 * Created by wangchen on 2017/5/29.
 */
@ServerEndpoint(value = "/websocket/virtualMachineShare/{stuorteaId}")
public class VirtualMachineShareWebSocket {

    private static final Logger logger = LoggerFactory.getLogger(VirtualMachineShareWebSocket.class);

    private static List<VirtualMachineShareWebSocket> connectionList = new ArrayList<VirtualMachineShareWebSocket>();
    private Session session;
    private String stuorteaId;


    @OnOpen
    public void onOpen(@PathParam("stuorteaId") String stuorteaId, Session session) {
        connectionList.add(this);
        this.session = session;
        this.stuorteaId = stuorteaId;
    }

    @OnClose
    public void onClose() {
        connectionList.remove(this);
    }

    @OnMessage
    public void onMessage(String jsonStr, Session session) {
    }

    @OnError
    public void onError(Session session, Throwable error) {
        logger.error("error", error);
        connectionList.remove(this);
    }

    public static void broadcast(String userName, String vmId, List<String> shareStuorteaIdList, Integer marks) throws OpenstackException{
        String url = OpenstackUtil.queryConsole(vmId);
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("userName", userName);
        resultMap.put("url", url);
        resultMap.put("marks", marks);
        resultMap.put("vmId", vmId);
        resultMap.put("share", true);
        try {
            String reJsonStr = mapper.writeValueAsString(resultMap);
            for (String stuorteaId : shareStuorteaIdList) {
                for (VirtualMachineShareWebSocket ws : connectionList) {
                    if (stuorteaId.equals(ws.stuorteaId)) {
                        try {
                            ws.session.getBasicRemote().sendText(reJsonStr);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public static Set<String> getMemberSet() {
        Set<String> memberSet = new HashSet<String>();
        for (VirtualMachineShareWebSocket ws : connectionList) {
            memberSet.add(ws.getStuorteaId());
        }
        return memberSet;
    }

    public String getStuorteaId() {
        return stuorteaId;
    }

}
